﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SPUserPoll.Presentation.WebParts
{
    public class FeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = null;
            try
            {
                webApp = properties.Feature.Parent as SPWebApplication;
                AddHttpHandler(webApp, properties.Feature.DefinitionId.ToString());
            }
            catch (Exception ex)
            {
                webApp.Features.Remove(properties.Feature.DefinitionId, true);
                throw new SPException("Error adding web.config properties for reporting, activation aborted. " + ex.Message);
            }
            base.FeatureActivated(properties);
        }

        public static void AddHttpHandler(SPWebApplication webApp, string owner)
        {
            //add to system.web/httpHandlers - for classic IIS web app's
            SPWebConfigModification httpHandlerElement = new SPWebConfigModification();
            httpHandlerElement.Name = "add[@path='ChartImg.axd'][@verb='GET,HEAD,POST'][@type='System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'][@validate='false']";
            httpHandlerElement.Path = "configuration/system.web/httpHandlers";
            httpHandlerElement.Sequence = 0;
            httpHandlerElement.Owner = owner;
            httpHandlerElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            httpHandlerElement.Value = "<add path=\"ChartImg.axd\" verb=\"GET,HEAD,POST\" type=\"System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\" validate=\"false\" />";
            webApp.WebConfigModifications.Add(httpHandlerElement);

            //add configuration/system.web/compilation/assemblies
            SPWebConfigModification assemblyElement = new SPWebConfigModification();
            assemblyElement.Name = "add[@assembly='System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35']";
            assemblyElement.Path = "configuration/system.web/compilation/assemblies";
            assemblyElement.Sequence = 0;
            assemblyElement.Owner = owner;
            assemblyElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            assemblyElement.Value = "<add assembly=\"System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35\"/>";
            webApp.WebConfigModifications.Add(assemblyElement);

            //add to system.webServer/handlers - for native/integrated IIS web apps
            SPWebConfigModification handlersElement = new SPWebConfigModification();
            handlersElement.Name = "add[@name='ChartImg'][@verb='*'][@path='ChartImg.axd'][@type='System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']";
            handlersElement.Path = "configuration/system.webServer/handlers";
            handlersElement.Sequence = 0;
            handlersElement.Owner = owner;
            handlersElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlersElement.Value = "<add name=\"ChartImg\" verb=\"*\" path=\"ChartImg.axd\"  type=\"System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"  />";
            webApp.WebConfigModifications.Add(handlersElement);

            //add temp image storage configuration configuration/appSettings     <add key="ChartImageHandler" value="storage=file;timeout=20;dir=C:\temp;" />
            SPWebConfigModification imgStorage = new SPWebConfigModification();
            imgStorage.Name = "add[@key='ChartImageHandler'][@value='storage=file;timeout=20;']";
            imgStorage.Path = "configuration/system.webServer/handlers";
            imgStorage.Sequence = 0;
            imgStorage.Owner = owner;
            imgStorage.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            imgStorage.Value = "<add key=\"ChartImageHandler\" value=\"storage=session;timeout=20;\" />";
            webApp.WebConfigModifications.Add(imgStorage);

            webApp.Update();
            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        }

        public static bool HandlerPresent(SPWebApplication webApp, string Path, string CompareValue)
        {
            try
            {
                System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/", webApp.Name);
                System.Configuration.ConfigurationSection section = config.GetSection(Path);
                if (section != null)
                {
                    if (section.ToString().ToUpper().Contains(CompareValue.ToUpper()))
                        return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }
    }
}